function return_module(data) {
    ajaxLoaderClose();
    llenarPatrullero(data);
};
historyNav=new Array();
function printHistoryNav(position){
    historyNavText='<a href="'+HOST_PATHS+'votosmsinstituto">YARACUY</a>';
    for(i in historyNav){
        if(i<position){
            historyNavText+=' > <div ref="'+historyNav[i]['id']+'" class="styleLink '+historyNav[i]['action']+' close">'+historyNav[i]['name']+'</div>';
        }else{
            historyNavText+=' > <div ref="'+historyNav[i]['id']+'" class="styleLink" style="color: #EE3338; cursor: auto">'+historyNav[i]['name']+'</div>';
        }
    
    }
    $("#historyNav").html(historyNavText);
}
$(document).ready(function() {
    $("#ver-pdf").attr('href', HOST_PATHS+'votosms/pdf/index');
    $("#list-carga").tablesorter({
        sortList:[[0,0]], 
        widgets: ['zebra'], 
        headers: {
            3:{
                sorter: false
            }, 
            4:{
                sorter: false
            }
        }
    });
    $(".state").on({
        click: function(){
            pointer=this;
            var id=$(pointer).attr('ref');
            ajaxLoader();
            if($(pointer).hasClass('disable')){
                url=HOST_PATHS+'ccontrataciones/disable/'+id;
                text='Activar';
                addClass='enable';
                removeClass='disable';
            }else if($(pointer).hasClass('enable')){
                url=HOST_PATHS+'ccontrataciones/enable/'+id;
                text='Desactivar';
                addClass='disable';
                removeClass='enable';
            }
            $.ajax({ 
                type: "GET",
                dataType: 'json',
                url: url,
                success: function(data){
                    ajaxLoaderClose();
                    if(typeof data.result_error == "undefined" || data.result_error==false){
                        $(pointer).text(text);
                        $(pointer).removeClass(removeClass);
                        $(pointer).addClass(addClass);
                        fdialog(null,data.messageAction,'success');
                        tableContent();
                    }else{
                        fdialog(null,data.result_error,'message');
                    }
                }
            });
        }
    });
    var count = 0;
    $(".instituto").live({
        click: function(){
            pointer=this;
            var id=$(pointer).attr('ref');
            if($(pointer).hasClass('close')){
                ajaxLoader();
                url=HOST_PATHS+'votosmsinstituto/municipio/'+id;
                addClass='open';
                removeClass='close';
                $.ajax({ 
                    type: "GET",
                    dataType: 'json',
                    url: url,
                    success: function(data){
                        ajaxLoaderClose();
                        historyNav={
                            1:{
                                'id':id,
                                'action':'centro',
                                'name':$(pointer).text()
                            }
                        };
                        printHistoryNav(3);
                        $('#container_content .content #list-carga tfoot').remove();
                        var destinationTHEAD='#container_content .content #list-carga thead';
                        var contentTHEAD;
                        $(destinationTHEAD).html('');
                        contentTHEAD+='<tr class="ui-widget-header ">';
                        contentTHEAD+='<th rowspan="2">N°</th>';
                        contentTHEAD+='<th rowspan="2">Municipio</th>';
                        contentTHEAD+='<th rowspan="2">CNE</th>';
                        contentTHEAD+='<th rowspan="2">PSUV</th>';
                        contentTHEAD+='<th colspan="3">Responsables</th>';
                        contentTHEAD+='<th colspan="3">Miembro 1x10</th>';
                        contentTHEAD+='<th rowspan="2">Otros Votos</th>';
                        contentTHEAD+='<th rowspan="2">Total Votos</th>';
                        contentTHEAD+='<th rowspan="2">%</th>';
                        contentTHEAD+='</tr>';
                        contentTHEAD+='<tr class="ui-widget-header ">';
                        contentTHEAD+='<th>Cant</th>';
                        contentTHEAD+='<th>Votaron</th>';
                        contentTHEAD+='<th>%</th>';
                        contentTHEAD+='<th>Cant</th>';
                        contentTHEAD+='<th>Votaron</th>';
                        contentTHEAD+='<th>%</th>';
                        contentTHEAD+='</tr>';
                        $(destinationTHEAD).html(contentTHEAD);
                        var destinationTBODY='#container_content .content #list-carga tbody';
                        var contentTBODY;
                        $(destinationTBODY).html('');
                        for (var i = 0; i < data.length; i++)
                        {
                            var n=i+1;
                            contentTBODY+='<tr>';
                            contentTBODY+='<td>' + n +'</td>';
                            contentTBODY+='<td><div ref="' + data[i].cod_municipio+'" class="styleLink municipio close">' + data[i].municipio+'</div></td>';
                            contentTBODY+='<td>' + data[i].electores+'</td>';
                            contentTBODY+='<td>' + data[i].electores_PSUV+'</td>';
                            contentTBODY+='<td>' + data[i].cant_responsable1x10+'</td>';
                            contentTBODY+='<td>' + data[i].voto_responsable1x10+'</td>';
                            contentTBODY+='<td>' + '%'+'</td>';
                            contentTBODY+='<td>' + data[i].cant_miembro1x10+'</td>';
                            contentTBODY+='<td>' + data[i].voto_miembro1x10+'</td>';
                            contentTBODY+='<td>' + '%'+'</td>';
                            contentTBODY+='<td>' + data[i].otros_votos+'</td>';
                            contentTBODY+='<td>' + data[i].votos+'</td>';
                            contentTBODY+='<td>' + '%'+'</td>';
                            contentTBODY+='</tr>';
                        }
                        $(destinationTBODY).html(contentTBODY);
                        $("#list-carga").tablesorter({
                            widgets: ['zebra'],
                            headers: {
                                4:{
                                    sorter: false
                                }
                            }
                        });
                        $("#list-carga").trigger("update");
                    }
                });
            }else{
                url=HOST_PATHS+'ccontrataciones/enable/'+id;
                addClass='close';
                removeClass='open';
                $(pointer.parentNode.parentNode.nextSibling).remove();
            }
            $(pointer).removeClass(removeClass);
            $(pointer).addClass(addClass);
            $("#ver-pdf").attr('href', HOST_PATHS+'votosms/pdf/patrulla/'+id);
        }
    });
    $(".municipio").live({
        click: function(){
            pointer=this;
            var id=$(pointer).attr('ref');
            if($(pointer).hasClass('close')){
                ajaxLoader();
                url=HOST_PATHS+'votosms/parroquia/'+id;
                addClass='open';
                removeClass='close';
                $.ajax({ 
                    type: "GET",
                    dataType: 'html',
                    url: url,
                    success: function(data){
                        ajaxLoaderClose();
                        historyNav={
                            1:{
                                'id':id,
                                'action':'municipio',
                                'name':$(pointer).text()
                            }
                        };
                        printHistoryNav(1);
                        $('#container_content .content #list-carga tfoot').remove();
                        $('#container_content .content #list-carga tbody').remove();
                        $('#titleCol1').html('Parroquia');
                        $("#historyNav").css('visibility', 'visible');
                        $('#container_content .content #list-carga').append(data);
                        $("#list-carga").tablesorter({
                            widgets: ['zebra']
                        });
                        $("#list-carga").trigger("update");
                    }
                });
            }else{
                url=HOST_PATHS+'ccontrataciones/enable/'+id;
                addClass='close';
                removeClass='open';
                $(pointer.parentNode.parentNode.nextSibling).remove();
            }
            $(pointer).removeClass(removeClass);
            $(pointer).addClass(addClass);
            $("#ver-pdf").attr('href', HOST_PATHS+'votosms/pdf/parroquia/'+id);
        }
    });
    $(".parroquia").live({
        click: function(){
            pointer=this;
            var id=$(pointer).attr('ref');
            if($(pointer).hasClass('close')){
                ajaxLoader();
                url=HOST_PATHS+'votosms/centro/'+id;
                addClass='open';
                removeClass='close';
                $.ajax({ 
                    type: "GET",
                    dataType: 'html',
                    url: url,
                    success: function(data){
                        ajaxLoaderClose();
                        historyNav={
                            1: historyNav[1],
                            2:{
                                'id':id,
                                'action':'parroquia',
                                'name':$(pointer).text()
                            }
                        };
                        printHistoryNav(2);
                        $('#container_content .content #list-carga tfoot').remove();
                        $('#container_content .content #list-carga tbody').remove();
                        $('#titleCol1').html('Centro');
                        $('#container_content .content #list-carga').append(data);
                        $("#list-carga").tablesorter({
                            widgets: ['zebra']
                        });
                        $("#list-carga").trigger("update");
                    }
                });
            }else{
                url=HOST_PATHS+'ccontrataciones/enable/'+id;
                addClass='close';
                removeClass='open';
                $(pointer.parentNode.parentNode.nextSibling).remove();
            }
            $(pointer).removeClass(removeClass);
            $(pointer).addClass(addClass);
            $("#ver-pdf").attr('href', HOST_PATHS+'votosms/pdf/centro/'+id);
        }
    });
    $(".centro").live({
        click: function(){
            pointer=this;
            var id=$(pointer).attr('ref');
            if($(pointer).hasClass('close')){
                ajaxLoader();
                url=HOST_PATHS+'votosms/patrulla/'+id;
                addClass='open';
                removeClass='close';
                $.ajax({ 
                    type: "GET",
                    dataType: 'json',
                    url: url,
                    success: function(data){
                        ajaxLoaderClose();
                        historyNav={
                            1: historyNav[1],
                            2: historyNav[2],
                            3:{
                                'id':id,
                                'action':'centro',
                                'name':$(pointer).text()
                            }
                        };
                        printHistoryNav(3);
                        $('#container_content .content #list-carga tfoot').remove();
                        var destinationTHEAD='#container_content .content #list-carga thead';
                        var contentTHEAD;
                        $(destinationTHEAD).html('');
                        contentTHEAD+='<tr class="ui-widget-header ">';
                        contentTHEAD+='<th rowspan="2">N°</th>';
                        contentTHEAD+='<th rowspan="2">Cedula</th>';
                        contentTHEAD+='<th rowspan="2">Nombre</th>';
                        contentTHEAD+='<th rowspan="2">Telefono</th>';
                        contentTHEAD+='<th colspan="2">Miembros</th>';
                        contentTHEAD+='<th rowspan="2">Voto</th>';
                        contentTHEAD+='</tr>';
                        contentTHEAD+='<tr class="ui-widget-header ">';
                        contentTHEAD+='<th>Cant</th>';
                        contentTHEAD+='<th>Votaron</th>';
                        contentTHEAD+='</tr>';
                        $(destinationTHEAD).html(contentTHEAD);
                        var destinationTBODY='#container_content .content #list-carga tbody';
                        var contentTBODY;
                        $(destinationTBODY).html('');
                        for (var i = 0; i < data.length; i++)
                        {
                            var n=i+1;
                            contentTBODY+='<tr>';
                            contentTBODY+='<td>' + n +'</td>';
                            contentTBODY+='<td>' + data[i].cedula+'</td>';
                            contentTBODY+='<td><div ref="' + data[i].cedula+'" class="styleLink patrullero close">' + data[i].nombre+'</div></td>';
                            contentTBODY+='<td>' + data[i].telefono+'</td>';
                            contentTBODY+='<td>' + data[i].miembros+'</td>';
                            contentTBODY+='<td>' + data[i].votaron+'</td>';
                            contentTBODY+='<td>' + data[i].voto+'</td>';
                            contentTBODY+='</tr>';
                        }
                        $(destinationTBODY).html(contentTBODY);
                        $("#list-carga").tablesorter({
                            widgets: ['zebra'],
                            headers: {
                                4:{
                                    sorter: false
                                }
                            }
                        });
                        $("#list-carga").trigger("update");
                    }
                });
            }else{
                url=HOST_PATHS+'ccontrataciones/enable/'+id;
                addClass='close';
                removeClass='open';
                $(pointer.parentNode.parentNode.nextSibling).remove();
            }
            $(pointer).removeClass(removeClass);
            $(pointer).addClass(addClass);
            $("#ver-pdf").attr('href', HOST_PATHS+'votosms/pdf/patrulla/'+id);
        }
    });
    $(".patrullero").live({
        click: function(){
            pointer=this;
            var id=$(pointer).attr('ref');
            if($(pointer).hasClass('close')){
                ajaxLoader();
                url=HOST_PATHS+'votosms/patrullero/'+id;
                addClass='open';
                removeClass='close';
                $.ajax({ 
                    type: "GET",
                    dataType: 'json',
                    url: url,
                    success: function(data){
                        historyNav={
                            1: historyNav[1],
                            2: historyNav[2],
                            3: historyNav[3],
                            4:{
                                'id':id,
                                'action':'centro',
                                'name':$(pointer).text()
                            }
                        };
                        printHistoryNav(4);
                        llenarPatrullero(data);
                    }
                });
            }else{
                url=HOST_PATHS+'ccontrataciones/enable/'+id;
                addClass='close';
                removeClass='open';
                $(pointer.parentNode.parentNode.nextSibling).remove();
            }
            $(pointer).removeClass(removeClass);
            $(pointer).addClass(addClass);
            
            $("#ver-pdf").attr('href', HOST_PATHS+'votosms/pdf/patrullero/'+id);
        }
    });
    $(".print").live({
        click: function(){
            pointer=this;

            ajaxLoader();
            url=HOST_PATHS+'votosmsinstituto/sendpdf';
            addClass='open';
            removeClass='close';
            $.ajax({ 
                type: "POST",
                dataType: 'html',
                data:{
                    'data':$('#dataPrint').html()
                },
                url: url,
                success: function(){
                    ajaxLoaderClose();
                    var win = window.open(HOST_PATHS+'votosmsinstituto/printpdf/');
                //win.document.write(data);
                }
            });
        }
    });

});
function llenarPatrullero(data){
    if(data.length<1){
        fdialog(null,'No se encontro ninguna patrulla perteneciente a este numero de cedula','message')
    }else{
        ajaxLoaderClose();
        $('#container_content .content #list-carga tfoot').remove();
        var destinationTHEAD='#container_content .content #list-carga thead';
        var contentTHEAD;
        $(destinationTHEAD).html('');
        contentTHEAD+='<tr class="ui-widget-header ">';
        contentTHEAD+='<th>N°</th>';
        contentTHEAD+='<th>Cedula</th>';
        contentTHEAD+='<th>Nombre</th>';
        contentTHEAD+='<th>Telefono</th>';
        contentTHEAD+='<th>Voto</th>';
        contentTHEAD+='</tr>';
        $(destinationTHEAD).html(contentTHEAD);
        var destinationTBODY='#container_content .content #list-carga tbody';
        var contentTBODY;
        $(destinationTBODY).html('');
        for (var i = 0; i < data.length; i++)
        {
            var n=i+1;
            contentTBODY+='<tr>';
            contentTBODY+='<td>' + n +'</td>';
            contentTBODY+='<td>' + data[i].cedula+'</td>';
            contentTBODY+='<td>' + data[i].nombre+'</td>';
            contentTBODY+='<td>' + data[i].telefono+'</td>';
            contentTBODY+='<td>' + data[i].voto+'</td>';
            contentTBODY+='</tr>';
        }
        $(destinationTBODY).html(contentTBODY);
        $("#list-carga").tablesorter({
            widgets: ['zebra']
        });
        $("#list-carga").trigger("update");
    }
}
$('#carga_reporte_centro').live({
    submit: function(event){
        ajaxLoader();
        pointer=this;
        var url = $(pointer).attr('action');
        var data = $(pointer).serialize();
        pointer.reset();
        field=convertDataSerializeToArray(data);
        event.preventDefault();
        $.ajax({ 
            type: "POST",
            url: url,
            dataType: 'json',
            data: data,
            success: function(data){
                ajaxLoaderClose();
                if(typeof data.result_error == "undefined" || data.result_error==false){
                    fdialog(null,data.messageAction,'success');
                    $(pointer).find('.listMethodController').append('<dt><strong>'+data.name+'</strong> <div ref="' + data.id + '" class="styleLink removeMethod">X</div></dt>');
                }else{
                    fdialog(null,data.result_error,'message');
                }
                $(pointer.parentNode).remove();
            }
        });
    }
});

